import { createRouter, createWebHashHistory } from 'vue-router'
import { afterGuard, beforeGuard } from '@/router/guard'
import { IModules, IModulesItem } from '@/router/types'

const modules = import.meta.glob('./modules/*.ts', { eager: true }) as IModules
const routes = Object.values(modules).reduce(
  (total: RouteRecordRaw[], cur: IModulesItem) => {
    return total.concat(...cur.default)
  },
  []
)

export * from '@/types'

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior: () => {
    return { top: 0, left: 0 }
  },
})

beforeGuard(router)
afterGuard(router)

export default (app: App) => {
  app.use(router)
}
